package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.base.RecordApi;
import com.jmp.base.dto.*;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CollectionMapperExt;
import one.stand.model.ResultModel;
import one.stand.service.PersonalService;
import one.stand.service.RecordPublicService;
import one.stand.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cjr
 */
@RestController
public class RecordApiController implements RecordApi {

    @Autowired
    private RecordPublicService recordPublicService;

    @Autowired
    private RecordService recordService;
    @Autowired
    private PersonalService personalService;
    @Autowired
    private CollectionMapperExt collectionMapperExt;

    @Override
    public ResultModel call(@RequestBody RecordCallRequest request) {
        recordService.addCallsPhoneRecord(request.getUserId(), request.getCardId());
        return ResultModel.success();
    }

    @Override
    public ResultModel consult(@RequestBody RecordConsultRequest request) {
        recordService.addConsultRecord(request.getUserId(), request.getCardId(), request.getType());
        return ResultModel.success();
    }

    @Override
    public ResultModel<RecordCollectionVo> collection(@RequestBody RecordCollectionDto request) {
        boolean isCollection = recordPublicService.isCollection(request.getUserId(), request.getItemType(), request.getItemId());
        if (isCollection) {
            personalService.delCollection(request.getUserId(), request.getItemType(), request.getItemId());
        } else {
            personalService.addCollection(request.getUserId(), request.getItemType(), request.getItemId());
        }

        RecordCollectionVo vo = RecordCollectionVo.builder().flag(!isCollection).build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel<RecordCollectListVo> collectList(@RequestBody RecordCollectListDto request) {

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<RecordCollectListVo.Collect> list = collectionMapperExt.selectListByType(request.getUserId(), request.getType());
        PageResponse2<RecordCollectListVo.Collect> pageResponse2 = ConvertUtils.page2(list, recordCollectListVo -> recordCollectListVo);

        RecordCollectListVo vo = RecordCollectListVo.builder()
                .page(pageResponse2)
                .build();
        return ResultModel.success(vo);
    }
}
